package com.ddl.controller.core;

import com.ddl.common.exception.ExceptionUtil;
import com.ddl.common.exception.ResultModel;
import com.ddl.controller.UserController;
import com.ddl.service.StagePropertyService;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2018-12-01
 */
@RestController
@RequestMapping(value = "/app/prop")
public class ShopController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);


    @Autowired
    StagePropertyService stagePropertyService;

    /**
     * 商城道具
     * @param request
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/getPropList")
    public ResultModel getPropList(HttpServletRequest request) {
        try{
            return stagePropertyService.getPropList();
        }catch (Exception e){
            logger.error("getPropList fail", e);
            return ExceptionUtil.getMessageException(e);
        }
    }

    /**
     * 购买道具
     * @param map
     * @param request
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/submitOrder")
    public ResultModel submitOrder(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        try {
            String userId = request.getHeader("userId");
            String propertyId = map.get("propertyId").toString();
            int count = Integer.parseInt(map.get("count").toString());
            return stagePropertyService.submitOrder(userId, propertyId, count);
        } catch (Exception e) {
            logger.error("submitOrder fail, map:{}", new Gson().toJson(map), e);
            return ExceptionUtil.getMessageException(e);
        }
    }

    /**
     * 我的道具
     *
     * @param request
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/geUserPropList")
    public ResultModel geUserPropList(HttpServletRequest request) {
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            return stagePropertyService.geUserPropList(userId);
        } catch (Exception e) {
            logger.error("geUserPropList fail", e);
            return ExceptionUtil.getMessageException(e);
        }
    }
}
